<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">

      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            size="small"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              size="small"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" size="small" @click="">添加管理员</el-button>
        </el-col>
      </el-row>
      <el-table
        :data="admins"
        style="width: 100%">
        <el-table-column
          header-align="center"
          align="center"
          type="index"
          label="序号"
        >
        </el-table-column>
        <el-table-column
          prop="username"
          label="用户名"
          width="180">
        </el-table-column>
        <el-table-column
          prop="password"
          label="密码"
          width="180">
        </el-table-column>
        <el-table-column
          align="center"
          label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status"
                       active-color="#13ce66"
                       inactive-color="#ff4949"
                       :active-value="1"
                       :inactive-value="0"
                       inactive-text="禁用"
                       active-text="正常"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="操作"
        >

          <template slot-scope="scope">
            <el-button type="danger" size="mini">删除</el-button>
            <el-button type="warning" size="mini">编辑</el-button>
            <el-button type="primary" size="mini" @click="openAuthorizeDialog(scope.row.id)">授权</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      title="授权"
      :visible.sync="authorizeDialogVisible"
      width="60%"
      >
      <div>
        <el-transfer
          v-model="adminRoles"
          :titles="['未选角色','已有角色']"
          :button-texts="['移除角色','添加角色']"
          :props="{key:'roleId',label:'roleName'}"
          :data="roles">
        </el-transfer>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="authorizeDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="authorize">确 定</el-button>
      </span>
    </el-dialog>
  </div>

</template>

<script>
export default {
  name: "BzMallAdmin",
  data(){
    return {
      authorizeDialogVisible:false,
      admins:[],
      roles:[],
      adminRoles:[],
      adminId:null
    }
  },
  methods:{
    openAuthorizeDialog(id){
      this.adminId = id;
      this.axios.get("/admin/roles").then(resp=>{
          this.roles = resp.data.data;
          this.axios.get(`/admin/roles/${this.adminId}`).then((resp)=>{
            this.adminRoles = resp.data.data.map(item=>item.roleId);
            this.authorizeDialogVisible = true;
        })
      })
    },
    authorize(){
      this.axios.put(`/admin/roles/${this.adminId}`,this.adminRoles)
        .then(resp=>{
          this.authorizeDialogVisible=false;
          if (resp.data.message == "success") {
            this.$message({
              type:"success",
              message:"授权成功"
            })
          }else{
            this.$message({
              type:"error",
              message:"授权失败,请稍后重试"
            })
          }
        })
    }
  },
  created(){
    this.axios.get("/admin/admins")
      .then(resp=>{
        this.admins = resp.data.data;
      })
  }
}
</script>

<style scoped>
  .el-card{
    margin-top:20px
  }
  .el-row{
    margin: 10px 0;
  }
</style>
